package xcmg.device.web.purchase;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.purchase.BuyPlanService;
import xcmg.device.service.vo.FunctionResult;
import xcmg.device.service.vo.purchase.BuyPlanAddVO;
import xcmg.device.service.vo.purchase.BuyPlanReportVO;
import xcmg.device.service.vo.purchase.BuyPlanVO;
import yb.ecp.fast.dto.UserDetailDTO;
import yb.ecp.fast.feign.AuthClient;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.Arrays;

@RestController
@RequestMapping("/buyPlan")
public class BuyPlanController extends BasicController {

    @Autowired
    private BuyPlanService buyPlanService;

    @Autowired
    private AuthClient authClient;

    @Value( "${spring.file.download.path}" )
    private String filePath;

    @Autowired
    private HttpServletResponse response;

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<BuyPlanVO> condition)throws Exception{
        BuyPlanVO buyPlanVO = condition.getFilters();
        buyPlanVO.setOrgId(getOrgId());
        condition.setFilters(buyPlanVO);
        PageCommonVO pageCommonVO = buyPlanService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/autoCreate/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult autoCreatePlanLists(@RequestBody SearchCommonVO<BuyPlanVO> condition)throws Exception{
        BuyPlanVO buyPlanVO = condition.getFilters();
        buyPlanVO.setOrgId(getOrgId());
        condition.setFilters(buyPlanVO);
        PageCommonVO pageCommonVO = buyPlanService.autoCreatePlanLists(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/reportList",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult reportList(@RequestBody SearchCommonVO<BuyPlanReportVO> condition)throws Exception{
        BuyPlanReportVO reportVO = condition.getFilters();
        reportVO.setOrgId(getOrgId());
        condition.setFilters(reportVO);
        PageCommonVO pageCommonVO = buyPlanService.reportList(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult add(@RequestBody BuyPlanAddVO buyPlanAddVO)throws Exception{
        BuyPlanVO buyPlanVO = buyPlanAddVO.getHeaderInfo();
        buyPlanVO.setCreateId(getUserId());
        buyPlanVO.setCreateName(getUserName());
        buyPlanVO.setOrgId(getOrgId());
        buyPlanAddVO.setHeaderInfo(buyPlanVO);
        try {
            return actionResult(buyPlanService.add(buyPlanAddVO));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult item(String id){
        return actionValueResult(buyPlanService.item(id));
    }

    @RequestMapping(value = "/itemForPurchase",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult itemForPurchase(String id) throws Exception {
        String userId = getUserId();
        String orgId = getOrgId();
        ActionResult<UserDetailDTO> userDetailRes = authClient.getUserDetail(userId);
        UserDetailDTO userDetail = userDetailRes.getValue();
        String purchaseGroup = userDetail.getPurchaseGroup();
        return actionValueResult(buyPlanService.itemForPurchase(id,purchaseGroup,orgId));
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult update(@RequestBody BuyPlanAddVO buyPlanAddVO)throws Exception{
        try {
            return actionResult(buyPlanService.update(buyPlanAddVO));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult remove(@RequestBody String[] ids)throws Exception{
        if(ids==null||ids.length==0){
            return actionResult(ErrorCode.IllegalArument);
        }
        try {
            return actionResult(buyPlanService.delete(Arrays.asList(ids)));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/audit",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult audit(@RequestBody BuyPlanVO buyPlanVO)throws Exception{
        try {
            return actionResult(buyPlanService.audit(buyPlanVO, getUserId()));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/export",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void export( String[] ids)throws Exception {
        buyPlanService.export(Arrays.asList(ids));
    }

    @RequestMapping(value = "/exportAll",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void exportAll(BuyPlanVO condition)throws Exception {
        condition.setOrgId(getOrgId());
        buyPlanService.exportAll(condition);
    }

    @ResponseBody
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult importExcel(MultipartHttpServletRequest multipartRequest) throws Exception {
        MultipartFile file = multipartRequest.getFile(multipartRequest.getFileNames().next());
        String fileName = file.getOriginalFilename();
        FunctionResult<String> result = new FunctionResult<>();
        if (!fileName.trim().endsWith(".xls")) {
            result.setCode(ErrorCode.ImportExcelEndWithXLS);
        } else {
            InputStream in = file.getInputStream();
            result = buyPlanService.importExcel(in, getOrgId());
        }
        return actionResult(result.getCode(), result.getValue());
    }

    @RequestMapping(value = "/download", method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void download() throws Exception {
        buyPlanService.download();
    }

    @ResponseBody
    @ApiOperation( value = "导入采购计划" )
    @RequestMapping( value = "/importBuyPlan", method = RequestMethod.POST )
    @FastMappingInfo( needLogin = true )
    public ActionResult importBuyPlanExcel( MultipartHttpServletRequest multipartRequest ) throws Exception {
        MultipartFile file = multipartRequest.getFile( multipartRequest.getFileNames().next() );
        String fileName = file.getOriginalFilename();
        FunctionResult<Object> result = new FunctionResult<>();
        if (!fileName.trim().endsWith( ".xls" ))
        {
            result.setCode( ErrorCode.ImportExcelEndWithXLS );
        }
        else
        {
            InputStream in = file.getInputStream();
            result = buyPlanService.importBuyPlanExcel( in, getOrgId(), getUserId(), getUserName(), getDeptId(), getDeptName() );
        }
        return actionResult( result.getCode(), result.getValue() );
    }


    @ApiOperation( value = "导入采购计划模板下载" )
    @RequestMapping( value = "/downloadBuyPlan", method = RequestMethod.POST )
    @FastMappingInfo( needLogin = true )
    public void downloadBuyPlan() throws Exception {
        buyPlanService.downloadBuyPlan();
    }

    @ApiOperation( value = "结单" )
    @RequestMapping(value="/manualFinish", method = RequestMethod.POST )
    @FastMappingInfo( needLogin = true )
    public ActionResult manualFinish(@RequestBody BuyPlanVO buyPlanVO) {
        if (buyPlanVO.getMaterialsNo() == null) {
            return actionResult(buyPlanService.manualFinish(buyPlanVO.getId()));
        } else {
            return actionResult(buyPlanService.manualFinishDetail(buyPlanVO.getId(), buyPlanVO.getMaterialsNo()));
        }
    }

}
